import numpy as np
import matplotlib as mpl
import seaborn as sns
from scipy.stats.kde import gaussian_kde
from scipy import stats


class PlotSeaborn(object):
    def __init__(self):
        # matplotlib中文显示方块
        mpl.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
        mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
        self.plt = mpl.pyplot

    def seaborn_mat(self, n=250):
        """
        KDE图
        :param N:
        :return:
        """
        # 生成250个随机数
        np.random.seed(n)
        data = np.random.randn(n)

        # 画出kde图
        sns.distplot(a=data, color='#ff8000')

        self.plt.title('KDE图', fontsize=15)
        self.plt.show()

    def seaborn_scipy(self):
        """
        scipy numpy seaborn KDE图
        :return:
        """
        # 正态分布随机数
        sample1 = stats.norm.rvs(loc=-1.0, scale=1, size=320)
        sample2 = stats.norm.rvs(loc=2.0, scale=0.6, size=320)
        # 两个数组合并
        sample = np.hstack([sample1, sample2])
        # gaussian_kde函数
        pdf = gaussian_kde(sample)

        x = np.linspace(-5, 5, 200)
        y = pdf(x)

        # line
        self.plt.plot(x, y, 'r')
        # hist
        self.plt.hist(sample, normed=1, alpha=0.45, color='purple')

        # hist + line
        # sns.distplot(a=sample)

        self.plt.title('scipy numpy seaborn KDE图')
        self.plt.show()



plot_seaborn = PlotSeaborn()
plot_seaborn.seaborn_scipy()
